﻿using System;
using Microsoft.Xna.Framework;

namespace LightSystemTest
{
    public class SpotLight : Light
    {

        public Vector2 direction;
        public float innerAngle;
        public float outerAngle;

        public bool obtuse = false;

        public SpotLight(Vector2 pos, Vector2 dir, float inner, float outer, float power, float _radius, Color col)
            : base(pos, power, col)
        {
            direction = dir;
            innerAngle = inner;
            outerAngle = outer;
            radius = _radius;
            if (outerAngle > MathHelper.PiOver2)
                obtuse = true;
        }

        public SpotLight(Vector2 pos, Vector2 dir, float angle, float power, Color col)
            : base(pos, power, col)
        {
            direction = dir;
            innerAngle = angle;
            outerAngle = angle;
            if (outerAngle > MathHelper.PiOver2)
                obtuse = true;
        }

        public SpotLight(Vector2 pos, Vector2 dir, float angle, Color col)
            : base(pos, 0f, col)
        {
            direction = dir;
            innerAngle = outerAngle = angle;
            if (outerAngle > MathHelper.PiOver2)
                obtuse = true;
        }

        public float GetAngleBias()
        {
            float diffAngle = (float)Math.Acos(Vector2.Dot(direction, Vector2.UnitY));
            if (float.IsNaN(diffAngle))
                diffAngle = (float)(((Math.Sign(-direction.Y) + 1) / 2f) * Math.PI);
            if (diffAngle - (outerAngle / 2f) < 0)
                return 0;
            return MathHelper.Pi * 2f;
        }
    }
}
